<?php
namespace App\Api\Controllers\lianfuyouzheng;


use App\Models\TfConfig;
use App\Models\TfStore;
use function EasyWeChat\Kernel\Support\get_client_ip;
use Illuminate\Support\Facades\Log;
use Rtgm\sm\RtSm2;
use Illuminate\Support\Facades\DB;

class PayController extends BaseController
{

    //扫一扫 0-系统错误 1-成功 2-正在支付 3-失败
    public function scan_pay($data)
    {
        try {
            $out_trade_no = $data['out_trade_no'];
            $code = $data['code'];
            $total_amount = $data['total_amount'];
            $apikey = $data['apikey'];
            $signkey = $data['signkey'];

            $goods_name = $out_trade_no;
            $pos_sn = $data['pos_sn'];
            $orderData=json_encode(array('orderFlag'=>'0','orderTitle'=>'日常消费','orderAMT'=>sprintf('%.2f',$total_amount)));
            $termInfo=array('deviceType'=>'13','deviceId'=>$pos_sn,'orderAMT'=>sprintf('%.2f',$total_amount));
            $buff='{';
            foreach($termInfo as $k=>$v){
                $buff=$buff.'\"'.$k.'\"'.':'.'\"'.$v.'\",';
            }
            $buff=rtrim($buff,"," );
            
            $termInfo=$buff.'}';
            $device=DB::table('devices')->where('store_id', $data['store_id'])->where('device_type','v_you_pay')->first();
            //邮储发起交易
            
            $store=DB::table('stores')->where('store_id',$data['store_id'])->first();
            $lianfuyoupay_stores=DB::table('lianfuyoupay_stores')->where('store_id', $data['store_id'])->first();
            if(empty($lianfuyoupay_stores)){
                $f_store=DB::table('stores')->where('id',$store->pid)->first();
                $lianfuyoupay_stores=DB::table('lianfuyoupay_stores')->where('store_id', $f_store->store_id)->first();
            }
            if($lianfuyoupay_stores->type==3){
                $platformID='300370800006480';
                $certNum='1451489200';
                $reqSysId='GIZ00001307';
                $prikey='6a5dc556f72f8f186216e2cf2cfadbc70a4f900083234e10f5c9c5affc36842a';
                $pubkey='04b60bf1e7a12bff87a4ecc130b472f15c915e6ff18010a3c6f1c19979649ef03be8e4c2561eef8a473491ee8131d908967219a6d34999284cb82d6bfa3a883f18';
            }else if($lianfuyoupay_stores->type==4){
                $platformID='300340100007387';
                $certNum='1488619724';
                $reqSysId='GIZ00001507';
                $prikey='0221b5cf7eb87fdc2c4a45c239989a9a5765cbeb04935a90fa51dc2dd23d0b37';
                $pubkey='0491ff67c7d2f56fb4eb943d84178b297448fad4ed9756e9318362977a87692318183aad374f15830d88a1f9265f746db68e4b41a7f6089105d510bf83d9af4e04';
            }else if($lianfuyoupay_stores->type==5){
                $platformID='300610100008704';
                $certNum='1541933287';
                $reqSysId='GIZ00001643';
                $prikey='64c63a510a6e9c943cd5caa8eb05cc3edf5694a6ee5eb38bcde15591196f0d9c';
                $pubkey='04074c885e27268777bdb1d326a88fa022a0b0e1994674a4070953a3dc93391348899c5769b6bb224e659388a5aca2ef00f6202999d01740ea34627ca320d3c695';
            }else{
                if($store->user_id=='236'){
                    $platformID='300610100004854';
                    $certNum='1424820092';
                    $reqSysId='GIZ00000958';
                    $prikey='82ff7a19133bda1d4a2c9460c57763625bcd95de102f3edb5556be278050cfb8';
                    $pubkey='78232c3993b5f1e4ef55559c6db86ad1180d8f8308f22d51ca77ec398fa83110f5dba5c69a22336cf7704aeacc1283e823157342ded1c5d7e24c8bd57d248e92';
                }else{
                    $platformID='300340100002864';
                    $certNum='1390171054';
                    $reqSysId='GIZ00000520';
                    $prikey='e3155bb90c1b4cbef128bd21cf6326dd45747f1445c0d05c4d2f30492567f348';
                    $pubkey='337A144065FED2DFC8A6CC191F72A66B68BB72EB981775C0275ECA9FC9421FF024F691DDC6D2078C072FE33404DD8B875A5F5ECE1A70A522869549F0478D9F75';
                }
            }
            
            $post_data = [
                'version' => '1.0',
                'txnCode' => '8003',
                'channelID' => '08',
                'platformID' => $platformID,
                'reqTraceID' => $out_trade_no,
                'reqDate' => date('YmdHis',time()),
                'certNum' => $certNum,
                'reqSysId' => $reqSysId,
                'isSign' => '1',
                'mchtNo'=>$apikey,//
                'txnAMT'=>sprintf('%.2f',$total_amount),//
                'currencyCode'=>'156',//
                'qrCode'=>$code,//
                'orderData'=>$orderData,//
                'termID'=>'Wft01187',//
                'termInfo'=>$termInfo,//
                'whetherNotify'=>'01',//
            ];
            
            //单品
            error_log(date('Y-m-d H:i:s')." 【邮储单品】： ".json_encode([$data, $store->is_goods])."\n\n", 3, "/www/wwwlogs/wft/". 'prod-youchu'.date('Ymd').".log");
            if (isset($data['goods_detail']) && !empty($data['goods_detail']) && $store->is_goods == 1) {
                 $goods = json_decode($data['goods_detail'], true);
                 $goods['receipt_id'] = (string)$goods['receipt_id'];
                 foreach ($goods['goods_detail'] as &$gv) {
                     $gv['goods_id'] = (string)$gv['goods_id'];
                 }
                 $post_data['detailWX']= addslashes(json_encode($goods));
            }
            
            if(!empty($device)){
                $post_data['whetherCh']='00';
                $post_data['cloudHornSn']=$device->device_no;
            }
            $post_data = array_filter($post_data);
            ksort($post_data);//整理数组排序
            $string=$this->get_sign($post_data);
            $sm2 = new RtSm2('base64');
            $sign = $sm2->doSign(
                $string,
                $prikey,
                $pubkey,
                '1234567812345678');
            $sign=trim($sign);
            $post_data['signature']=$sign;
            $json='{';
            foreach($post_data as $k=>$v){
                if($k=='orderData'){
                    $json=$json.'"'.$k.'"'.':'.$v.',';
                }else if($k=='termInfo'){
                    $json=$json.'"'.$k.'"'.':'.'"'.$v.'",';
                }else{
                    $json=$json.'"'.$k.'"'.':'.'"'.$v.'",';
                }
            }
            $json=rtrim($json,"," );
            $json=$json.'}';
            $url = 'https://nap.psbc.com/trans/intermgr/online/api/platform/qrserviceNet/orderPay';
            $obj = new BaseController();
            // error_log(date('Y-m-d H:i:s')." 【邮储入参】： ".json_encode($json)."\n\n", 3, "/tmp/". 'youchu'.date('Ymd').".log");
//            error_log(date('Y-m-d H:i:s')." 【邮储入参】： ".json_encode($json)."\n\n", 3, "/www/wwwlogs/wft/". 'prod-youchu'.date('Ymd').".log");
            $re = $this->http_curl1($url, $json);
            Log::channel("pay")->info("邮储响应:",[
                "request"=>$json,
                "response"=>$re
            ]);
            if ($lianfuyoupay_stores->type==4){
                Log::channel("youchu")->info("邮储机构类型-".$lianfuyoupay_stores->type,[
                    "reqData"=>$json,
                    "resData"=>$re
                ]);
            }
//            error_log(date('Y-m-d H:i:s')." 【邮储响应】： ".json_encode($re)."\n\n", 3, "/www/wwwlogs/wft/". 'prod-youchu'.date('Ymd').".log");
           //  error_log(date('Y-m-d H:i:s')." 【邮储响应】： ".json_encode($re)."\n\n", 3, "/tmp/". 'youchu'.date('Ymd').".log");

            $re = json_decode($re, true);

            //交易成功
            if ($re['respCd'] == '0000') {
                return [
                    'status' => 1,
                    'message' => '交易成功',
                    'data' => $re,
                ];
            } //用户输入密码
            elseif ($re['respCd'] == '9607'||$re['respCd'] == '9030'||$re['respCd'] == '9022') {
                return [
                    'status' => 2,
                    'message' => '请用户输入密码',
                    'data' => $re,
                ];
            } else {
                return [
                    'status' => 3,
                    'message' => $re,
                ];
            }
        } catch (\Exception $exception) {
            return [
                'status' => 0,
                'message' => $exception->getMessage()
            ];
        }
    }


    //查询订单 0-系统错误 1-成功 2-正在支付 3-失败 4.已经退款 5 退款中
    public function order_query($data)
    {
        try {
            $out_trade_no = $data['out_trade_no'];
            $apikey = $data['apikey'];
            $signkey = $data['signkey'];
            $pos_sn = $data['pos_sn'];
            $order=DB::table('orders')->where('out_trade_no',$out_trade_no)->first();
            $store=DB::table('stores')->where('store_id',$order->store_id)->first();
            $lianfuyoupay_stores=DB::table('lianfuyoupay_stores')->where('store_id', $data['store_id'])->first();
            if(empty($lianfuyoupay_stores)){
                $f_store=DB::table('stores')->where('id',$store->pid)->first();
                $lianfuyoupay_stores=DB::table('lianfuyoupay_stores')->where('store_id', $f_store->store_id)->first();
            }
            if($lianfuyoupay_stores->type==3){
                $platformID='300370800006480';
                $certNum='1451489200';
                $reqSysId='GIZ00001307';
                $prikey='6a5dc556f72f8f186216e2cf2cfadbc70a4f900083234e10f5c9c5affc36842a';
                $pubkey='04b60bf1e7a12bff87a4ecc130b472f15c915e6ff18010a3c6f1c19979649ef03be8e4c2561eef8a473491ee8131d908967219a6d34999284cb82d6bfa3a883f18';
            }else if($lianfuyoupay_stores->type==4){
                $platformID='300340100007387';
                $certNum='1488619724';
                $reqSysId='GIZ00001507';
                $prikey='0221b5cf7eb87fdc2c4a45c239989a9a5765cbeb04935a90fa51dc2dd23d0b37';
                $pubkey='0491ff67c7d2f56fb4eb943d84178b297448fad4ed9756e9318362977a87692318183aad374f15830d88a1f9265f746db68e4b41a7f6089105d510bf83d9af4e04';
            }else if($lianfuyoupay_stores->type==5){
                $platformID='300610100008704';
                $certNum='1541933287';
                $reqSysId='GIZ00001643';
                $prikey='64c63a510a6e9c943cd5caa8eb05cc3edf5694a6ee5eb38bcde15591196f0d9c';
                $pubkey='04074c885e27268777bdb1d326a88fa022a0b0e1994674a4070953a3dc93391348899c5769b6bb224e659388a5aca2ef00f6202999d01740ea34627ca320d3c695';
            }else{
                if($store->user_id=='236'){
                    $platformID='300610100004854';
                    $certNum='1424820092';
                    $reqSysId='GIZ00000958';
                    $prikey='82ff7a19133bda1d4a2c9460c57763625bcd95de102f3edb5556be278050cfb8';
                    $pubkey='78232c3993b5f1e4ef55559c6db86ad1180d8f8308f22d51ca77ec398fa83110f5dba5c69a22336cf7704aeacc1283e823157342ded1c5d7e24c8bd57d248e92';
                }else{
                    $platformID='300340100002864';
                    $certNum='1390171054';
                    $reqSysId='GIZ00000520';
                    $prikey='e3155bb90c1b4cbef128bd21cf6326dd45747f1445c0d05c4d2f30492567f348';
                    $pubkey='337A144065FED2DFC8A6CC191F72A66B68BB72EB981775C0275ECA9FC9421FF024F691DDC6D2078C072FE33404DD8B875A5F5ECE1A70A522869549F0478D9F75';
                }
            }
            
            //邮储发起交易
            $post_data = [
                'version' => '1.0',
                'txnCode' => '8006',
                'channelID' => '08',
                'platformID' => $platformID,
                'reqTraceID' => $out_trade_no,
                'reqDate' => date('YmdHis',time()),
                'certNum' => $certNum,
                'reqSysId' => $reqSysId,
                'isSign' => '1',
                'mchtNo'=>$apikey,//
                'orgReqTraceID'=>$out_trade_no,//
            ];
            $post_data = array_filter($post_data);
            ksort($post_data);//整理数组排序
            $string=$this->get_sign($post_data);
            $sm2 = new RtSm2('base64');
            $sign = $sm2->doSign(
                $string,
                $prikey,
                $pubkey,
                '1234567812345678');
            $sign=trim($sign);
            $post_data['signature']=$sign;
            $json='{';
            foreach($post_data as $k=>$v){
                $json=$json.'"'.$k.'"'.':'.'"'.$v.'",';
            }
            $json=rtrim($json,"," );
            $json=$json.'}';
            
            $url = 'https://nap.psbc.com/trans/intermgr/online/api/platform/qrserviceNet/orderQuery';
            $obj = new BaseController();
            $re = $this->http_curl1($url, $json);
            //file_put_contents('453453.log',var_export($re,true)."\r\n",FILE_APPEND);
            $re = json_decode($re, true);
            Log::channel("youchu")->info("order_query",[
                "url"=>$url,
                "req"=>$json,
                "res"=>$re
            ]);
            //交易成功
            if ($re['respCd'] == '0000') {
                if($re['txnSta']=='03'){
                    return [
                        'status' => 1,
                        'message' => '交易成功',
                        'data' => $re,
                    ];
                }else if($re['txnSta']=='01'||$re['txnSta']=='02'||$re['txnSta']=='99'){
                    return [
                        'status' => 2,
                        'message' => '请用户输入密码',
                        'data' => $re,
                    ];
                }else{
                     return [
                        'status' => 3,
                        'message' => $re,
                    ];
                }
            } //用户输入密码
            elseif ($re['respCd'] == '9607'||$re['respCd'] == '9030'||$re['respCd'] == '9022') {
                return [
                    'status' => 2,
                    'message' => '请用户输入密码',
                    'data' => $re,
                ];
            } else {
                return [
                    'status' => 3,
                    'message' => $re,
                ];
            }
        } catch (\Exception $exception) {
            return [
                'status' => 0,
                'message' => $exception->getMessage()
            ];
        }
    }


   //退款 0-系统错误 1-成功
    public function refund($data)
    {
        try {
            $out_trade_no = $data['out_trade_no'];
            $apikey = $data['apikey'];
            $signkey = $data['signkey'];
            $pay_amount = $data['pay_amount'];
            $reject_no = $data['refund_no'];
            $pos_sn = $data['pos_sn'];
            $reqTraceID=round(0000000,9999999).time();
            $order=DB::table('orders')->where('out_trade_no',$out_trade_no)->first();
            $store=DB::table('stores')->where('store_id',$order->store_id)->first();
            $lianfuyoupay_stores=DB::table('lianfuyoupay_stores')->where('store_id', $data['store_id'])->first();
            if(empty($lianfuyoupay_stores)){
                $f_store=DB::table('stores')->where('id',$store->pid)->first();
                $lianfuyoupay_stores=DB::table('lianfuyoupay_stores')->where('store_id', $f_store->store_id)->first();
            }
            if($lianfuyoupay_stores->type==3){
                $platformID='300370800006480';
                $certNum='1451489200';
                $reqSysId='GIZ00001307';
                $prikey='6a5dc556f72f8f186216e2cf2cfadbc70a4f900083234e10f5c9c5affc36842a';
                $pubkey='04b60bf1e7a12bff87a4ecc130b472f15c915e6ff18010a3c6f1c19979649ef03be8e4c2561eef8a473491ee8131d908967219a6d34999284cb82d6bfa3a883f18';
            }else if($lianfuyoupay_stores->type==4){
                $platformID='300340100007387';
                $certNum='1488619724';
                $reqSysId='GIZ00001507';
                $prikey='0221b5cf7eb87fdc2c4a45c239989a9a5765cbeb04935a90fa51dc2dd23d0b37';
                $pubkey='0491ff67c7d2f56fb4eb943d84178b297448fad4ed9756e9318362977a87692318183aad374f15830d88a1f9265f746db68e4b41a7f6089105d510bf83d9af4e04';
            }else if($lianfuyoupay_stores->type==5){
                $platformID='300610100008704';
                $certNum='1541933287';
                $reqSysId='GIZ00001643';
                $prikey='64c63a510a6e9c943cd5caa8eb05cc3edf5694a6ee5eb38bcde15591196f0d9c';
                $pubkey='04074c885e27268777bdb1d326a88fa022a0b0e1994674a4070953a3dc93391348899c5769b6bb224e659388a5aca2ef00f6202999d01740ea34627ca320d3c695';
            }else{
                if($store->user_id=='236'){
                    $platformID='300610100004854';
                    $certNum='1424820092';
                    $reqSysId='GIZ00000958';
                    $prikey='82ff7a19133bda1d4a2c9460c57763625bcd95de102f3edb5556be278050cfb8';
                    $pubkey='78232c3993b5f1e4ef55559c6db86ad1180d8f8308f22d51ca77ec398fa83110f5dba5c69a22336cf7704aeacc1283e823157342ded1c5d7e24c8bd57d248e92';
                }else{
                    $platformID='300340100002864';
                    $certNum='1390171054';
                    $reqSysId='GIZ00000520';
                    $prikey='e3155bb90c1b4cbef128bd21cf6326dd45747f1445c0d05c4d2f30492567f348';
                    $pubkey='337A144065FED2DFC8A6CC191F72A66B68BB72EB981775C0275ECA9FC9421FF024F691DDC6D2078C072FE33404DD8B875A5F5ECE1A70A522869549F0478D9F75';
                }
            }
            //邮储发起交易
            $post_data = [
                'version' => '1.0',
                'txnCode' => '8005',
                'channelID' => '08',
                'platformID' => $platformID,
                'reqTraceID' => $reqTraceID,
                'reqDate' => date('YmdHis',time()),
                'certNum' => $certNum,
                'reqSysId' => $reqSysId,
                'isSign' => '1',
                'mchtNo'=>$apikey,//
                'orgReqTraceID'=>$out_trade_no,//
                'txnAMT'=>number_format($pay_amount,2, '.', ''),
                'whetherNotify'=>'01'
            ];
            $post_data = array_filter($post_data);
            ksort($post_data);//整理数组排序
            $string=$this->get_sign($post_data);
            $sm2 = new RtSm2('base64');
            $sign = $sm2->doSign(
                $string,
                $prikey,
                $pubkey,
                '1234567812345678');
            $sign=trim($sign);
            $post_data['signature']=$sign;
            $json='{';
            foreach($post_data as $k=>$v){
                $json=$json.'"'.$k.'"'.':'.'"'.$v.'",';
            }
            $json=rtrim($json,"," );
            $json=$json.'}';
            $url = 'https://nap.psbc.com/trans/intermgr/online/api/platform/qrserviceNet/orderRefund';
            $obj = new BaseController();
            $this->add_refund_orders($reqTraceID,$out_trade_no,$pay_amount,2);
            $re = $this->http_curl1($url, $json);
            file_put_contents('b45645asd.log',var_export($re,true)."\r\n",FILE_APPEND);
            $re = json_decode($re, true);
            //交易成功
            if ($re['respCd'] == '0000') {
                return [
                    'status' => 1,
                    'message' => '交易成功',
                    'data' => $re,
                ];
            } else {
                return [
                    'status' => 3,
                    'message' => $re['respDesc']??"",
                ];
            }
        } catch (\Exception $exception) {
            return [
                'status' => 0,
                'message' => $exception->getMessage() . $exception->getLine()
            ];
        }
    }


    //退款查询 0-系统错误 1-成功 2-正在退款 3-失败
    public function refund_query($data)
    {
        try {
            $refund_trade_no = $data['refund_trade_no'];
            $sub_mch_id = $data['sub_mch_id'];
            $mch_id = $data['mch_id'];
            $pub_key = $data['pub_key'];
            $pri_key = $data['pri_key'];
            $date = $data['date'];

            $post_data = [
                'sub_mch_id' => $sub_mch_id,
                'refund_trade_no' => $refund_trade_no,
                'date' => $date,
            ];

            $method = '/openapi/merchant/pay/refund-query';
            $obj = new BaseController();
            $obj->mch_id = $mch_id;
            $obj->pub_key = $pub_key;
            $obj->pri_key = $pri_key;
            $re = $obj->api($post_data, $method, false);
            //系统错误
            if ($re['code'] != "0") {
                return [
                    'status' => 0,
                    'message' => $re['msg'],

                ];
            }
            //业务成功
            if ($re['data']['trade_status'] != 3) {
                return [
                    'status' => 1,
                    'message' => '退款成功',
                    'data' => $re['data'],
                ];
            } else {
                return [
                    'status' => 0,
                    'message' => '退款失败',
                    'data' => $re['data'],
                ];
            }
        } catch (\Exception $exception) {
            return [
                'status' => 0,
                'message' => $exception->getMessage()
            ];
        }
    }


    //生成动态二维码-公共
    public function send_qr($data)
    {
        try {
            $out_trade_no = $data['out_trade_no'];
            $total_amount = $data['total_amount'];
            $remark = $data['remark'];
            $notify_url = $data['notify_url'];
            $url = $data['request_url'];
            $returnParams = $data['return_params']; //原样返回
            $md_key = $data['md_key'];
            $mid = $data['mid'];
            $orgNo = $data['orgNo'];
            $payChannel = $data['payChannel'];

            //请求数据
            $data = [
                'mid' => $mid,
                'totalFee' => $total_amount,
                'outTradeNo' => $out_trade_no,
                'nonceStr' => $out_trade_no,
                'payChannel' => $payChannel,
                'notifyUrl' => $notify_url,
                'orgNo' => $orgNo,
            ];

            //和融通交易接口
            $this->md_key = $md_key;
            $re = $this->execute($data, $url);
            //系统错误
            if ($re['resultCode'] == "fail") {
                return [
                    'status' => 0,
                    'message' => $re['errDes'],

                ];
            }

            if ($re['resultCode'] == "success") {
                return [
                    'status' => 1,
                    'code_url' => $re['codeUrl'],
                    'message' => $re,

                ];
            }
        } catch (\Exception $exception) {
            return [
                'status' => 0,
                'message' => $exception->getMessage(),
            ];
        }
    }


    //静态码提交-公共
    // public function qr_submit($data)
    // {
    //     try {
    //         $out_trade_no = $data['out_trade_no'];
    //         $open_id = $data['open_id'];
    //         $total_amount = $data['total_amount'];
    //         $remark = $data['remark'];
    //         $device_id = $data['device_id'];
    //         $shop_name = $data['shop_name'];
    //         $notify_url = $data['notify_url'];

    //         $sub_mch_id = $data['sub_mch_id'];
    //         $mch_id = $data['mch_id'];
    //         $pub_key = $data['pub_key'];
    //         $pri_key = $data['pri_key'];
    //         $channel = $data['channel'];
    //         $post_data = [
    //             'sub_mch_id' => $sub_mch_id,
    //             'channel' => $channel,
    //             'client_ip' => get_client_ip(),
    //             'total_fee' => $total_amount,
    //             'out_trade_no' => $out_trade_no,
    //             'body' => $shop_name,
    //             'store_id' => $sub_mch_id,
    //             'terminal_id' => $device_id,
    //             'openid' => $open_id,
    //             'notify_url' => $notify_url,
    //         ];

    //         if ($channel == "WECHAT_MP") {
    //             $post_data['wx_appid'] = $data['wx_appid'];
    //         }
    //         $method = '/openapi/merchant/pay/gateway';
    //         $obj = new BaseController();
    //         $obj->mch_id = $mch_id;
    //         $obj->pub_key = $pub_key;
    //         $obj->pri_key = $pri_key;
    //         $re = $obj->api($post_data, $method, false);

    //         //系统错误
    //         if ($re['code'] != "0") {
    //             return [
    //                 'status' => 0,
    //                 'message' => $re['msg']
    //             ];
    //         }

    //         //如果是失败报备一下
    //         if (isset($re['data']['trade_status']) && $re['data']['trade_status'] == 6) {
    //             $method = '/openapi/merchant/open';

    //             $TfStore = TfStore::where('sub_mch_id', $sub_mch_id)
    //                 ->select('config_id', 'qd')
    //                 ->first();

    //             $TfConfig = TfConfig::where('config_id', $TfStore->config_id)
    //                 ->where('qd', $TfStore->qd)
    //                 ->select('alipay_pid', 'wechat_channel_no')
    //                 ->first();
    //             if (!$TfConfig) {
    //                 $TfConfig = TfConfig::where('config_id', '1234')
    //                     ->where('qd', $TfStore->qd)
    //                     ->select('alipay_pid', 'wechat_channel_no')
    //                     ->first();
    //             }

    //             //传化支付宝报备
    //             $post_data = [
    //                 'sub_mch_id' => $sub_mch_id,
    //                 'channel' => '0',
    //                 'alipay_pid' => $TfConfig->alipay_pid
    //             ];

    //             $re1 = $obj->api($post_data, $method, false);
    //             //传化微信报备
    //             $post_data = [
    //                 'sub_mch_id' => $sub_mch_id,
    //                 'channel' => '1',
    //                 'wechat_channel_no' => $TfConfig->wechat_channel_no
    //             ];

    //             $re2 = $obj->api($post_data, $method, false);
    //         }

    //         return [
    //             'status' => 1,
    //             'message' => $re['msg'],
    //             'data' => $re['data']

    //         ];
    //     } catch (\Exception $exception) {
    //         return [
    //             'status' => 0,
    //             'message' => $exception->getMessage()
    //         ];
    //     }
    // }
    
    //静态码提交-公共
    public function qr_submit($data)
    {
        try {
            $out_trade_no = $data['out_trade_no'];
            $total_amount = $data['total_amount'];
            $apikey = $data['apikey'];
            $signkey = $data['signkey'];
            $pos_sn = $data['pos_sn'];
            $termInfo=array('deviceType'=>'13','deviceId'=>$pos_sn,'orderAMT'=>sprintf('%.2f',$total_amount));
            $buff='{';
            foreach($termInfo as $k=>$v){
                $buff=$buff.'\"'.$k.'\"'.':'.'\"'.$v.'\",';
            }
            $buff=rtrim($buff,"," );
            
            $termInfo=$buff.'}';
            $device=DB::table('devices')->where('store_id', $data['store_id'])->where('device_type','v_you_pay')->first();
            
            $store=DB::table('stores')->where('store_id',$data['store_id'])->first();
            
            $lianfuyoupay_stores=DB::table('lianfuyoupay_stores')->where('store_id', $data['store_id'])->first();
            if(empty($lianfuyoupay_stores)){
                $f_store=DB::table('stores')->where('id',$store->pid)->first();
                $lianfuyoupay_stores=DB::table('lianfuyoupay_stores')->where('store_id', $f_store->store_id)->first();
            }
            if($lianfuyoupay_stores->type==3){
                $platformID='300370800006480';
                $certNum='1451489200';
                $reqSysId='GIZ00001307';
                $prikey='6a5dc556f72f8f186216e2cf2cfadbc70a4f900083234e10f5c9c5affc36842a';
                $pubkey='04b60bf1e7a12bff87a4ecc130b472f15c915e6ff18010a3c6f1c19979649ef03be8e4c2561eef8a473491ee8131d908967219a6d34999284cb82d6bfa3a883f18';
            }elseif ($lianfuyoupay_stores->type==4){
                $platformID='300340100007387';
                $certNum='1488619724';
                $reqSysId='GIZ00001507';
                $prikey='0221b5cf7eb87fdc2c4a45c239989a9a5765cbeb04935a90fa51dc2dd23d0b37';
                $pubkey='0491ff67c7d2f56fb4eb943d84178b297448fad4ed9756e9318362977a87692318183aad374f15830d88a1f9265f746db68e4b41a7f6089105d510bf83d9af4e04';
            }else if($lianfuyoupay_stores->type==5){
                $platformID='300610100008704';
                $certNum='1541933287';
                $reqSysId='GIZ00001643';
                $prikey='64c63a510a6e9c943cd5caa8eb05cc3edf5694a6ee5eb38bcde15591196f0d9c';
                $pubkey='04074c885e27268777bdb1d326a88fa022a0b0e1994674a4070953a3dc93391348899c5769b6bb224e659388a5aca2ef00f6202999d01740ea34627ca320d3c695';
            }else{
                if($store->user_id=='236'){
                    $platformID='300610100004854';
                    $certNum='1424820092';
                    $reqSysId='GIZ00000958';
                    $prikey='82ff7a19133bda1d4a2c9460c57763625bcd95de102f3edb5556be278050cfb8';
                    $pubkey='78232c3993b5f1e4ef55559c6db86ad1180d8f8308f22d51ca77ec398fa83110f5dba5c69a22336cf7704aeacc1283e823157342ded1c5d7e24c8bd57d248e92';
                }else{
                    $platformID='300340100002864';
                    $certNum='1390171054';
                    $reqSysId='GIZ00000520';
                    $prikey='e3155bb90c1b4cbef128bd21cf6326dd45747f1445c0d05c4d2f30492567f348';
                    $pubkey='337A144065FED2DFC8A6CC191F72A66B68BB72EB981775C0275ECA9FC9421FF024F691DDC6D2078C072FE33404DD8B875A5F5ECE1A70A522869549F0478D9F75';
                }
            }
            //邮储发起交易
            $post_data = [
                'version' => '1.0',
                'txnCode' => '8002',
                'channelID' => '08',
                'platformID' => $platformID,
                'reqTraceID' => $out_trade_no,
                'reqDate' => date('YmdHis',time()),
                'certNum' => $certNum,
                'reqSysId' => $reqSysId,
                'isSign' => '1',
                'mchtNo'=>$apikey,//
                'txnAMT'=>sprintf('%.2f',$total_amount),//
                'whetherNotify'=>'00',
                'termInfo'=>$termInfo,//
                'backUrl'=>url('api/wincode/youpaycallback')//
            ];
            if(!empty($device)){
                $post_data['whetherCh']='00';
                $post_data['cloudHornSn']=$device->device_no;
            }
            $post_data = array_filter($post_data);
            ksort($post_data);//整理数组排序
            $string=$this->get_sign($post_data);
            $sm2 = new RtSm2('base64');
            $sign = $sm2->doSign(
                $string,
                $prikey,
                $pubkey,
                '1234567812345678');
            $sign=trim($sign);
            $post_data['signature']=$sign;
            $json='{';
            foreach($post_data as $k=>$v){
                $json=$json.'"'.$k.'"'.':'.'"'.$v.'",';
            }
            $json=rtrim($json,"," );
            $json=$json.'}';
            file_put_contents('b23967.log',var_export($json,true)."\r\n",FILE_APPEND);
            $url = 'https://nap.psbc.com/trans/intermgr/online/api/platform/qrserviceNet/reqQr';
            $obj = new BaseController();
            // file_put_contents('youce.log',var_export($json,true)."\r\n",FILE_APPEND);
            $re = $this->http_curl1($url, $json);
            file_put_contents('b23967.log',var_export($re,true)."\r\n",FILE_APPEND);
            // file_put_contents('youce.log',var_export($re,true)."\r\n",FILE_APPEND);
            $re = json_decode($re, true);
            if($re['respCd']=='0000'){
                return [
                    'status' => 1,
                    'message' => $re['respDesc'],
                    'data' => $re
                ];
            }else{
                return [
                    'status' => 2,
                    'message' => $re['respDesc'],
                    'data' => $re
                ];
            }
        } catch (\Exception $exception) {
            return [
                'status' => 0,
                'message' => $exception->getMessage()
            ];
        }
    }


    //订单关闭
    public function order_close($data)
    {
        try {
            $out_trade_no = $data['out_trade_no'];
            $url = $data['request_url'];
            $md_key = $data['md_key'];
            $mid = $data['mid'];
            $orgNo = $data['orgNo'];

            //请求数据
            $data = [
                'mid' => $mid,
                'nonceStr' => $out_trade_no,
                'outTradeNo' => $out_trade_no,
                'orgNo' => $orgNo,
            ];

            //和融通交易接口
            $this->md_key = $md_key;
            $re = $this->execute($data, $url);

            //系统错误
            if ($re['resultCode'] == "fail") {
                return [
                    'status' => 0,
                    'message' => $re['errDes']
                ];
            }

            //业务成功
            if ($re['resultCode'] == "success") {
                //交易成功
                return [
                    'status' => 1,
                    'message' => '关闭成功',
                    'data' => $re
                ];
            } else {
                return [
                    'status' => 0,
                    'message' => $re['errDes']
                ];
            }
        } catch (\Exception $exception) {
            return [
                'status' => 0,
                'message' => $exception->getMessage()
            ];
        }
    }


}
